#coding: utf-8
class CulturalHeritage::TipologiesController < ApplicationController
  #before_filter :authenticate_user!
  # GET /cultural_heritage/tipologies
  # GET /cultural_heritage/tipologies.xml
  def index
    @cultural_heritage_tipologies = CulturalHeritage::Tipology.all
    @title_view = 'Tipologías'
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_tipologies }
      format.json  { render :json => @cultural_heritage_tipologies }
    end
  end

  # GET /cultural_heritage/tipologies/1
  # GET /cultural_heritage/tipologies/1.xml
  def show
    @cultural_heritage_tipology = CulturalHeritage::Tipology.find(params[:id])
    @title_view = 'Tipologías'
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_tipology }
       format.json  { render :json => @cultural_heritage_tipology }
    end
  end

  # GET /cultural_heritage/tipologies/new
  # GET /cultural_heritage/tipologies/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_tipology = CulturalHeritage::Tipology.new
      @groups = CulturalHeritage::Group.find(:all,:conditions=>{:deleted=>[0]})
      @subgroups = CulturalHeritage::SubGroup.find(:all,:conditions=>{:deleted=>[0]})
      @title_view = 'Tipologías'
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_tipology }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/tipologies/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_tipology = CulturalHeritage::Tipology.find(params[:id])
      @groups = CulturalHeritage::Group.find(:all,:conditions=>{:deleted=>[0]})
      @subgroups = CulturalHeritage::SubGroup.find(:all,:conditions=>{:deleted=>[0]})
      @title_view = 'Tipologías'
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /cultural_heritage/tipologies
  # POST /cultural_heritage/tipologies.xml
  def create
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_tipology = CulturalHeritage::Tipology.new(params[:cultural_heritage_tipology])
      @title_view = 'Tipologías'
      respond_to do |format|
        if @cultural_heritage_tipology.save
          format.html { redirect_to(@cultural_heritage_tipology, :notice => 'Se ha creado correctamente la tipologia.') }
          format.xml  { render :xml => @cultural_heritage_tipology, :status => :created, :location => @cultural_heritage_tipology }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @cultural_heritage_tipology.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /cultural_heritage/tipologies/1
  # PUT /cultural_heritage/tipologies/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_tipology = CulturalHeritage::Tipology.find(params[:id])
      @title_view = 'Tipologías'
      respond_to do |format|
        if @cultural_heritage_tipology.update_attributes(params[:cultural_heritage_tipology])
          format.html { redirect_to(@cultural_heritage_tipology, :notice => 'Se ha actualizado correctamente la tipologia.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @cultural_heritage_tipology.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /cultural_heritage/tipologies/1
  # DELETE /cultural_heritage/tipologies/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
      @cultural_heritage_tipology = CulturalHeritage::Tipology.find(params[:id])
      #@cultural_heritage_tipology.destroy
      @cultural_heritage_tipology.deleted = 1
      @cultural_heritage_tipology.save
      respond_to do |format|
        format.html { redirect_to(cultural_heritage_tipologies_url) }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

end
